//
//  LSZVideoWebView+LCPingLun.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/12.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZVideoWebView+LCPingLun.h"
#import "LCHomePingLunTableViewController.h"

@implementation LSZVideoWebView (LCPingLun)

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString * strLong = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (strLong.length == 0) {
        self.uilabel.hidden = NO;
    }else{
        if (strLong.length > 0 &&self.uilabel.hidden == NO) {
            self.uilabel.hidden = YES;
        }
    }
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        NSLog(@"点击发送键");
        [self initWithUserCommentsOnResources:strLong];
        [self.view endEditing:YES];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    return YES;
}


#pragma mark - 进行评价
-(void)initWithUserCommentsOnResources :(NSString *)content {
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    NSString *resource_id,*type;
    if (self.searchKeyContentMg) {
        resource_id = self.searchKeyContentMg.id_id;
        type = self.searchKeyContentMg.type;
    }
    if (self.model) {
        resource_id = self.model.id_id;
        type = self.model.type;
    }
    //    NSLog(@"resource_id = %@", resource_id);
    //    NSLog(@"type = %@", type);
    ///监听键盘事件,回车进行发送评论
    [LSZRequestData UserCommentsOnResources:resource_id content:content type:type :^(BOOL isOK, NSString * Str, NSDictionary * dic) {
        if (isOK == YES) {
            //            self.uilabel.hidden = NO;
            self.textView.text = @"";
            //            _isJumpCommentDiv = YES;
            // 用于拼接JS代码的
            NSMutableString *JSString = [NSMutableString string];
            [JSString appendString:@"location.reload();"];
            //            [JSString appendString:[NSString stringWithFormat:@"  for(var t = Date.now();Date.now() - t <= 1000;);location.href='#comment';"]];//,self.webView.URL
            //             直接调用JS注入的方法即可
            [self.webView evaluateJavaScript:JSString completionHandler:nil];
            
            //            [self.webView.scrollView setContentOffset:CGPointMake(0, self.webView.scrollView.bounds.size.height) animated:YES];
            [SVProgressHUD showSuccessWithStatus:@"评论成功!"];
            [SVProgressHUD dismissWithDelay:1];
            
        }else{
            [SVProgressHUD showErrorWithStatus:Str];
            [SVProgressHUD dismissWithDelay:1];
        }
    }];
}

- (void)createPingLun{
    [self initWithAppraise:self.view share:1 :^(UIView * lowView,UITextView * textView,UILabel * label, UIButton * but, UIImageView *imageShare,UIImageView *imageKeep,UIImageView *imagePingLunNum) {
        textView.delegate = self;
        self.lowView = lowView;
        //        self.lowView.hidden = YES;
        self.textView = textView;
        label.text = @"写评论....";
        self.uilabel = label;
        [but addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }];
    
}
#pragma mark - //1有分享  其他没分享
- (void)initWithAppraise:(UIView *)view share:(NSInteger)share :(homeStrPinglLun)block {
    UIView * lowView = [[UIView alloc]init];
    lowView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lowView];
    if (iPhoneX) {
        [lowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(0);
            make.right.equalTo(view.mas_right).offset(0);
            make.bottom.equalTo(view.mas_bottom).offset(-15);
            make.height.mas_equalTo(@48);
        }];
    }else{
        [lowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(0);
            make.right.equalTo(view.mas_right).offset(0);
            make.bottom.equalTo(view.mas_bottom).offset(0);
            make.height.mas_equalTo(@48);
        }];
    }
    
    
    UIView * viewBackColor = [[UIView alloc]init];
    [viewBackColor.layer setMasksToBounds:YES];
    [viewBackColor.layer setCornerRadius:16.5];
    viewBackColor.backgroundColor = RGBColor(243, 243, 243);
    [lowView addSubview:viewBackColor];
    
    UIImageView * imageViewBi = [[UIImageView alloc]init];
    imageViewBi.image = [UIImage imageNamed:@"编辑"];
    [viewBackColor addSubview:imageViewBi];
    
    [imageViewBi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewBackColor.mas_left).offset(15);
        //        make.right.equalTo(viewBackColor.mas_right).offset(-60);
        make.bottom.equalTo(viewBackColor.mas_bottom).offset(-6.5);
        make.top.equalTo(viewBackColor.mas_top).offset(8.5);
        //        make.height.mas_equalTo(@15);
    }];
    
    UITextView * textView = [[UITextView alloc]init];
    //    textView. = @"点击此处说两句...";
    //    textView.scrollEnabled = NO;
    textView.scrollsToTop = NO;
    textView.font = [UIFont systemFontOfSize:14];
    textView.backgroundColor = RGBColor(243, 243, 243);
    //当textview的字符串为0时发送（rerurn）键无效
    textView.enablesReturnKeyAutomatically = YES;
    textView.keyboardType = UIKeyboardTypeDefault;
    //    //键盘return样式变成发送
    textView.returnKeyType = UIReturnKeySend;
    [viewBackColor addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewBackColor.mas_left).offset(35);
        make.right.equalTo(viewBackColor.mas_right).offset(-10);
        make.bottom.equalTo(viewBackColor.mas_bottom).offset(0);
        make.top.equalTo(viewBackColor.mas_top).offset(0);
        //        make.height.mas_equalTo(@15);
    }];
    UILabel * uilabel = [[UILabel alloc]init];
    uilabel.text = @"请填写详细地址";
    uilabel.enabled = NO;//lable必须设置为不可用
    uilabel.font = [UIFont systemFontOfSize:13];
    uilabel.backgroundColor = [UIColor clearColor];
    [textView addSubview:uilabel];
    //WithFrame:CGRectMake(2,3, textView.frame.size.width, 20
    [uilabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView.mas_left).offset(3);
        make.right.equalTo(textView.mas_right).offset(-0);
        make.bottom.equalTo(textView.mas_bottom).offset(-10);
        make.top.equalTo(textView.mas_top).offset(10);
        //        make.height.mas_equalTo(@15);
    }];
    
    UIButton * shareBut = [[UIButton alloc]init];
    [lowView addSubview:shareBut];
    if (share == 1) {
        shareBut.hidden = NO;
    }else{
        shareBut.hidden = YES;
    }
    [lowView addSubview:shareBut];
    if (share == 1) {
        UIView *loginView = [[UIView alloc] init];
        loginView.backgroundColor = arc4random_color;//调用随机色
        if (iPhoneX) {
            [lowView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lowView.mas_left).offset(0);
                make.right.equalTo(lowView.mas_right).offset(0);
                make.bottom.equalTo(lowView.mas_bottom).offset(-15);
                make.height.mas_equalTo(@48);
            }];
        }else{
            [lowView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lowView.mas_left).offset(0);
                make.right.equalTo(lowView.mas_right).offset(0);
                make.bottom.equalTo(lowView.mas_bottom).offset(0);
                make.height.mas_equalTo(@48);
            }];
        }
        [lowView addSubview:loginView];
    }
    
    [shareBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lowView.mas_right).offset(-10);
        make.bottom.equalTo(lowView.mas_bottom).offset(0);
        make.top.equalTo(lowView.mas_top).offset(0);
        make.width.mas_equalTo(@48);
    }];
    
    UIImageView * imageShare = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 20.5, 20.5)];
    imageShare.image = [UIImage imageNamed:@"分享"];
    [shareBut addSubview:imageShare];
    
    [imageShare mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(shareBut.mas_left).offset(15);
        make.bottom.equalTo(shareBut.mas_bottom).offset(-14.5);
        make.top.equalTo(shareBut.mas_top).offset(13);
        make.width.mas_equalTo(@20.5);
    }];
    
    ///收藏
    UIImageView * imageKeep = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 20.5, 20.5)];
    self.imageKeep = imageKeep;
    NSDictionary *json;
    if (self.searchKeyContentMg) {
        json = [self.searchKeyContentMg yy_modelToJSONObject];
    }
    if (self.model) {
        json = [self.model yy_modelToJSONObject];
    }
    self.isKeep = NO;
    self.imageKeep.image = [UIImage imageNamed:@"收藏"];
    
    for (NSInteger i = 0; i < self.selectedData.count; i++) {
        NSDictionary *dict = self.selectedData[i];
        if ([json[@"id_id"] isEqualToString:dict[@"id_id"]]) {
            self.imageKeep.image = [UIImage imageNamed:@"z4"];
            self.isKeep = YES;
        }
    }
    [lowView addSubview:imageKeep];
    
    [imageKeep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(shareBut.mas_left).offset(-13);
        make.bottom.equalTo(shareBut.mas_bottom).offset(-13.5);
        make.top.equalTo(shareBut.mas_top).offset(12);
        make.width.mas_equalTo(@22.5);
    }];
    
    ///评论数
    UIImageView *imagePingLunNum = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 20.5, 20.5)];
    imagePingLunNum.image = [UIImage imageNamed:@"留言"];
    [lowView addSubview:imagePingLunNum];
    
    [imagePingLunNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(imageKeep.mas_left).offset(-30);
        make.bottom.equalTo(imageKeep.mas_bottom);
        make.top.equalTo(shareBut.mas_top).offset(13);
        make.width.mas_equalTo(@20.5);
    }];
    self.pingLunNumLabel = [[UILabel alloc] init];
    self.pingLunNumLabel.text = @"0";
    self.pingLunNumLabel.textColor = [UIColor whiteColor];
    self.pingLunNumLabel.font = [UIFont systemFontOfSize:11];
    self.pingLunNumLabel.backgroundColor = [UIColor colorWithRed:0.94 green:0.33 blue:0.34 alpha:1.00];
    self.pingLunNumLabel.textAlignment = NSTextAlignmentCenter;
    self.pingLunNumLabel.layer.masksToBounds = YES;
    // 设置圆角大小
    self.pingLunNumLabel.layer.cornerRadius = 6.0 ;
    
    [imagePingLunNum addSubview:self.pingLunNumLabel];
    [self.pingLunNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imagePingLunNum.mas_top).offset(-5);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(12);
        make.centerX.equalTo(imagePingLunNum.mas_right);
    }];
    
    [viewBackColor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lowView.mas_left).offset(15);
        make.right.equalTo(imagePingLunNum.mas_left).offset(-30);
        make.bottom.equalTo(lowView.mas_bottom).offset(-8);
        make.top.equalTo(lowView.mas_top).offset(7.5);
    }];
    imagePingLunNum.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesturRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushPingLunVC)];
    [imagePingLunNum addGestureRecognizer:tapGesturRecognizer];
    
    imageKeep.userInteractionEnabled = YES;
    UITapGestureRecognizer *keepTapGesturRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shouchang)];
    [imageKeep addGestureRecognizer:keepTapGesturRecognizer];
    block(lowView,textView,uilabel,shareBut,imageShare,imageKeep,imagePingLunNum);
}
-(void)pushPingLunVC{
    NSString *resource_id,*type;
    if (self.searchKeyContentMg) {
        resource_id = self.searchKeyContentMg.id_id;
        type = self.searchKeyContentMg.type;
    }
    if (self.model) {
        resource_id = self.model.id_id;
        type = self.model.type;
    }
    
    LCHomePingLunTableViewController *homePingLunTableViewController = [[LCHomePingLunTableViewController alloc] init];
    homePingLunTableViewController.id_id = resource_id;
    homePingLunTableViewController.type = type;
    [self.navigationController pushViewController:homePingLunTableViewController animated:YES];
}
-(void)shouchang{
    NSLog(@"shouchang = ");
    
    NSDictionary *json;
    if (self.searchKeyContentMg) {
        json = [self.searchKeyContentMg yy_modelToJSONObject];
//        [self setStringWarning:@"搜索暂无法收藏!"];
//        return;
    }
    if (self.model) {
        json = [self.model yy_modelToJSONObject];
    }
    if (self.isKeep == NO) {
        [self setStringWarning:@"收藏成功!"];
        self.imageKeep.image = [UIImage imageNamed:@"z4"];
        [self.selectedData addObject:json];
        self.isKeep = YES;
    }else{
        [self.selectedData removeObject:json];
        [self setStringWarning:@"取消收藏!"];
        self.imageKeep.image = [UIImage imageNamed:@"收藏"];
        self.isKeep = NO;
    }
    if (self.searchKeyContentMg || self.model) {
        [[NSUserDefaults standardUserDefaults] setObject:self.selectedData.copy forKey:@"Videokeeps"];
    }
}

@end
